package mech;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import play.Things;

/**
 * Movement and attack input system.
 * Involves timers and move recognition from a move dictionary.
 * @author VincentLy
 */
public class AttackQ{

	public static CommandList list = new CommandList();
	private static String command = "";
	private static Move move;
	private static long startKey;
	private static long startTime;
	private static Timer refreshTimer = new Timer(250,new ActionListener(){
		public void actionPerformed(ActionEvent e){
			command = "";
			refreshTimer.stop();
		}
	});
	private static Timer moveTimer = new Timer(10,new ActionListener(){
		public void actionPerformed(ActionEvent e){
			Things.player.useMove(false);
			Things.player.fin();
			moveTimer.stop();
		}
	});
	
	/**
	 * Handles every new input and checks for viable move input sequences.
	 * @param s - sequence entered
	 * @author VincentLy
	 */
	public static void add(String s){
		if(!Things.player.isUsingMove()){
			//refreshing empty commands
			if(command==""){
				startKey = System.currentTimeMillis();
				refreshTimer.start();
			}	
			else
				refreshTimer.restart();
			//add input key
			if(s != "w")
				command+=s;		
			//check sequence
			//allows sequence if correctly input and is input fast enough
			if(list.containsKey(command) && (System.currentTimeMillis()-startKey)<list.get(command).exectime()){
				//execute the move
				move = list.get(command);
				list.execute(move);					
				Things.player.useMove(true);
				//trigger proper animations
				if(!(move.seq().equals("ss") || move.seq().equals("aa") || move.seq().equals("dd")))
					Things.player.shoot();			
				//allow the move cooldown to take effect
				moveTimer.setInitialDelay((int) move.time());
				startTime = System.currentTimeMillis();
				moveTimer.start();
				//refresh
				refreshTimer.stop();
				command = "";
			}
		}
		//allows move canceling
		else{		
			//allows dash canceled shot
			if(move == list.get("dd") || move == list.get("aa")){	
				if(s == "j"){
					Things.player.setXVel(0);
					move = list.get("j");
					list.execute(move);	
					Things.player.useMove(true);
					Things.player.shoot();
					moveTimer.stop();
					moveTimer.setInitialDelay((int) move.time());
					startTime = System.currentTimeMillis();
					moveTimer.start();
					command = "";					
				}
			}
		}
	}
	
	/**
	 * @return the start time
	 * @author VincentLy
	 */
	public static long time(){return startTime;}
	
	/**
	 * @return the initial delay
	 * @author VincentLy
	 */
	public static long delay(){return moveTimer.getInitialDelay();}
	
	/**
	 * @return updated input sequence
	 * @author VincentLy
	 */
	public static String input(){return "Input: " + command;}
	
	/**
	 * @return the move name
	 * @author VincentLy
	 */
	public static String lastMove(){
		if(move==null)
			return "";
		return move.name();
	}
}